# -*- encoding: utf-8 -*-

from django.http import HttpResponseRedirect, HttpResponse
from google.appengine.api import users

def login_required(fn):
    """checks to see if the user is logged in, if not, redirect to login """
    def _dec(view_func):
        def _checklogin(request, *args, **kwargs):
            user = users.get_current_user()
            if user:
                return view_func(request, *args, **kwargs)
            else:
                return HttpResponseRedirect(users.create_login_url(request.path))
        _checklogin.__doc__ = view_func.__doc__
        _checklogin.__dict__ = view_func.__dict__

        return _checklogin

    return _dec(fn)

def admin_required(fn):
    """checks to see if the user is logged in, if not, redirect to login """
    def _dec(view_func):
        def _checklogin(request, *args, **kwargs):
            if users.is_current_user_admin():
                return view_func(request, *args, **kwargs)
            else:
                return HttpResponse(status = 403)
        _checklogin.__doc__ = view_func.__doc__
        _checklogin.__dict__ = view_func.__dict__

        return _checklogin

    return _dec(fn)
